package day13.homework;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

public class Demo {
    static {//初始化
        input();
    }
    static Map<String,User> map=new HashMap<>();
    static Scanner sc=new Scanner(System.in);
    public static
    void main(String[] args) {
        one();
    }
    public static void one(){
        while (true){
            System.out.println("欢迎使用移动通卡系统");
            System.out.println("1.注册");
            System.out.println("2.登录");
            System.out.println("3.退出");
            System.out.println("4.打印所有用户信息");
            String input = sc.nextLine();
            int n;
            try {
                n = Integer.parseInt(input);
            } catch (NumberFormatException e) {
                System.out.println("请输入数字选项！");
                continue;
            }
            switch (n){
                case 1: demo();break;
                case 2: login();break;
                case 3: return;
                case 4:a();break;
            }
        }
    }
    public static void a(){
        for (Map.Entry<String, User> entry : map.entrySet()) {
            String key = entry.getKey();
            User value = entry.getValue();
            System.out.println(value);
        }
        System.out.println("-----------------------------");
    }
    public static void input() {
        try(FileInputStream fis = new FileInputStream("d:\\IO\\demo.aaa");
            ObjectInputStream ois = new ObjectInputStream(fis);) {
            map = (Map<String, User>) ois.readObject();
            System.out.println("读取成功");
            System.out.println("读取成功，用户数量：" + map.size());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void output()  {
        try (FileOutputStream fos=new FileOutputStream("d:\\IO\\demo.aaa");
             ObjectOutputStream oos=new ObjectOutputStream(fos);
            ){
            System.out.println("写入成功"+map.size());
            oos.writeObject(map);
            System.out.println("写入成功");
        } catch (IOException e) {
            System.err.println("保存数据失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    public static void login() {
        System.out.println("输入电话号码");
        String phone = sc.nextLine();
        System.out.println("输入密码");
        String password = sc.nextLine();
        User user = map.get(phone);
        if(user != null && user.getPassword().equals(password)) {
            System.out.println("用户实际密码: " + user.getPassword());
            System.out.println("登录成功");
            System.out.println(user);
        } else {
            System.out.println("手机号或密码错误");
        }
    }
    public static void demo(){
        User u = new User();
        Random r=new Random();
        String[] s=new String[9];
        System.out.println("选择卡号:");
        for (int i=1;i<10;i++){
            long l=r.nextInt(1000000000)+ 139000000000L;
            s[i-1]=l+"";
            System.out.print(i+". "+l +"\t");
            if(i%3==0) System.out.println();
        }
        System.out.println("选择卡号编号:");
        int n=sc.nextInt();
        u.setMobilePhoneNumber(s[n-1]);
        System.out.println("选择套餐:（1.话唠套餐、2.网虫套餐、3.超人套餐）");
        String str=sc.next();
        switch (str){
            case "1" : u.setPackageTarget(new Chatterbox());break;
            case "2" : u.setPackageTarget(new NetWorm());break;
            case "3" : u.setPackageTarget(new Superman());break;
        }
        System.out.println("输入用户名:");
        u.setName(sc.next());
        System.out.println("输入密码信息:");
        u.setPassword(sc.next());

        while (true) {
            System.out.println("输入预存话费金额:");
            double d=sc.nextDouble();
            if(d<((Package) u.getPackageTarget()).money){
                System.out.println("预存金额小于套餐金额,请重新输入");
            }else {
                u.setConsumptionAmount(((Package) u.getPackageTarget()).money);
                u.setCurrentBalance(d-((Package) u.getPackageTarget()).money);
                break;
            }
        }
        System.out.println("注册成功");
        map.put(u.getMobilePhoneNumber(),u);
        output();
        for (Map.Entry<String, User> entry : map.entrySet()) {
            User value = entry.getValue();
            System.out.println(value);
        }

    }
}
interface  Call{//通话接口
    public void call();
}
interface  TextMessage extends Call{//短信接口
    public
    void textMessage();
}
interface  SurfTheInternet extends TextMessage{//上网接口
    public
    void surfTheInternet();
}

class User implements Serializable {
    private static final long serialVersionUID = 1L;
    private String mobilePhoneNumber;
    private String name;
    private String password;
    private Package packageTarget;
    private double consumptionAmount;
    private double currentBalance;
    private int actualCallDuration;
    private int theNumberOfTrueShortMessages;
    public User(){}
    public  User(String mobilePhoneNumber,String name,String password,Package packageTarget,double consumptionAmount,double currentBalance,int actualCallDuration,int theNumberOfTrueShortMessages){
        this.mobilePhoneNumber=mobilePhoneNumber;
        this.name=name;
        this.password=password;
        this.packageTarget=packageTarget;
        this.consumptionAmount=consumptionAmount;
        this.currentBalance=currentBalance;
        this.actualCallDuration=actualCallDuration;
        this.theNumberOfTrueShortMessages=theNumberOfTrueShortMessages;
    }
    public String toString(){
       return "卡号"+"\t\t\t"+"用户名"+"\t"+"当前余额"+"\t"+"套餐信息"+"\n"+mobilePhoneNumber+"\t"+name+"\t\t"+currentBalance+"\t"+packageTarget;
    }
    public
    String getMobilePhoneNumber() {
        return mobilePhoneNumber;
    }
    public
    void setMobilePhoneNumber(String mobilePhoneNumber) {
        this.mobilePhoneNumber = mobilePhoneNumber;
    }
    public
    String getName() {
        return name;
    }
    public
    void setName(String name) {
        this.name = name;
    }
    public
    String getPassword() {
        return password;
    }
    public
    void setPassword(String password) {
        this.password = password;
    }
    public
    Object getPackageTarget() {
        return packageTarget;
    }
    public
    void setPackageTarget(Package packageTarget) {
        this.packageTarget = packageTarget;
    }
    public
    double getConsumptionAmount() {
        return consumptionAmount;
    }
    public
    void setConsumptionAmount(double consumptionAmount) {
        this.consumptionAmount = consumptionAmount;
    }
    public
    double getCurrentBalance() {
        return currentBalance;
    }
    public
    void setCurrentBalance(double currentBalance) {
        this.currentBalance = currentBalance;
    }
    public
    int getActualCallDuration() {
        return actualCallDuration;
    }
    public
    void setActualCallDuration(int actualCallDuration) {
        this.actualCallDuration = actualCallDuration;
    }
    public
    int getTheNumberOfTrueShortMessages() {
        return theNumberOfTrueShortMessages;
    }
    public
    void setTheNumberOfTrueShortMessages(int theNumberOfTrueShortMessages) {
        this.theNumberOfTrueShortMessages = theNumberOfTrueShortMessages;
    }
}